package com.woniuxy.intellihome.controller.admin;


import com.woniuxy.intellihome.entity.Category;
import com.woniuxy.intellihome.entity.GoodsSpu;
import com.woniuxy.intellihome.entity.ResponseEntity;
import com.woniuxy.intellihome.service.CategoryService;
import com.woniuxy.intellihome.service.GoodsSpuService;
import com.woniuxy.intellihome.utils.UtilsTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-08-11
 */
@RestController("admin/SPU")
@Slf4j
@RequestMapping("admin/SPU")
public class GoodsSpuController {
    @Autowired
    private GoodsSpuService goodsSpuService;
    @Autowired
    private CategoryService categoryService;

    @RequestMapping("list")
    public ResponseEntity SPUList(){
        List<GoodsSpu> goodsSpus = goodsSpuService.list();
        ResponseEntity<List<GoodsSpu>> entity = new ResponseEntity<>(goodsSpus);
        return entity;
    }
    @RequestMapping("getCategories")
    public ResponseEntity categoryList(){
        List<Category> categories = categoryService.list();
        ResponseEntity<List<Category>> entity = new ResponseEntity<>(categories);
        return entity;
    }
    @RequestMapping("add")
    public ResponseEntity add(@RequestBody GoodsSpu spu){
        spu.setStatus("y");
        spu.setNum(UtilsTools.getTimeNow());
        log.debug("SPU:{}",spu);
        goodsSpuService.save(spu);
        return new ResponseEntity(spu);
    }
    @RequestMapping("update")
    public ResponseEntity<GoodsSpu> update(@RequestBody GoodsSpu spu){
        log.debug("SPU:{}",spu);
        goodsSpuService.updateById(spu);
        return new ResponseEntity<>(spu);
    }

    //上下架
    @RequestMapping("upDown")
    public ResponseEntity changeStatus(int id){
        GoodsSpu spu = goodsSpuService.getById(id);
        if("y".equals( spu.getStatus())){
            spu.setStatus("n");
        }else{
            spu.setStatus("y");
        }
        goodsSpuService.updateById(spu);
        return new ResponseEntity<>(spu);
    }

}

